package com.example.smswatcher;

import android.app.Service;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.provider.ContactsContract;
import android.util.Log;


public class SMSWatcherService extends Service
{
	private ContentResolver cr =  null;
	private Cursor cursor = null;
	
	public void onCreate() 
	{
	    super.onCreate();
	    cr = getContentResolver();
	    cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
	}
	
	public int onStartCommand(Intent intent, int flags, int startId) 
	{  
		String state = intent.getStringExtra("state");
		String message = intent.getStringExtra("message");
		if(state.equals("run"))
		{
			 if (message.compareTo("STOLEN&&CONTACT") == 0)
			 {
				 eraseContact();
			 }
		}
	    return START_NOT_STICKY;
	}

	public void eraseContact()
	{
		while (cursor.moveToNext()) 
		{
			try
			{
				String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
				Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
				cr.delete(uri, null, null);
			}
			catch(Exception e)
			{
				Log.d("Erase", "FAIL erase");
			}
		}
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
}
